{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cf85c563",
   "metadata": {},
   "source": [
    "为什么我们需要这样的数据？它符合时序特征。这些数据用于机器学习和时间序列分析。\n",
    "\n",
    "这段代码定义了一个函数 `generate_dataset`，它基于正弦波生成一组数据。这个数据非常的好,因为比较容易理解,而且一目了然测试结果是否正确.\n",
    "\n",
    "函数中，`size` 参数定义了正弦波的长度，而 `timesteps` 定义了每个数据点考虑的时间步长。\n",
    "\n",
    "代码首先生成一个正弦波，然后基于这个波形创建一系列数据点。\n",
    "\n",
    "每个数据点包括一个时间步长内的波形（作为 `x`），和紧随其后的下一个值（作为 `y`）。\n",
    "\n",
    "最后，这些数据被转换为 NumPy 数组并重新整形以适应机器学习模型的输入要求。我们的学习目标意味着，我们模型预测也是这样的波形。\n",
    "\n",
    "不过为什么这里是使用正弦生成数据?这样的数据有什么特点?难道是拟合的结果也应该是一个正弦?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ead3c24f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c580cadf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function to generate a dataset based on a sine wave\n",
    "def generate_dataset(size=280, timesteps=25):\n",
    "    # Initialize the sine wave\n",
    "    sin_wave = np.sin(np.arange(size))\n",
    "\n",
    "    # Initialize lists to store the x and y values\n",
    "    x, y = [], []\n",
    "\n",
    "    # Loop through the sine wave to generate the dataset\n",
    "    for step in range(len(sin_wave) - timesteps):\n",
    "        # Append the timesteps of sine wave to x\n",
    "        x.append(sin_wave[step:step + timesteps])\n",
    "\n",
    "        # Append the next value in the sine wave to y\n",
    "        y.append(sin_wave[step + timesteps])\n",
    "\n",
    "    # Convert the lists to numpy arrays and reshape them\n",
    "    return np.array(x).reshape(len(y), timesteps, 1), np.array(y).reshape(len(y), 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f142079f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = generate_dataset()\n",
    "print(x.shape, y.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc20e951",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可视化\n",
    "plt.plot(x[-1])\n",
    "plt.plot(y)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
